package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.dao.model.PayAmountReport;
import com.xyy.saas.payment.dao.model.PayAmountReportQueryParam;
import com.xyy.saas.payment.pojo.bo.PayAmountReportBO;
import com.xyy.saas.payment.pojo.vo.PayAmountReportParamVO;
import com.xyy.saas.payment.manage.cores.vo.ResultPageVo;
import com.xyy.saas.payment.service.PayAmountReportService;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/payAmountReport")
public class PayAmountReportController {

    @Autowired
    private PayAmountReportService payAmountReportService;

    @PostMapping(value = "/query",produces = "application/json;charset=utf-8")
    @ResponseBody
    public ResultPageVo queryPayAmountReport(@RequestBody PayAmountReportParamVO queryParam){
        log.info("查询交易对账信息参数:{}", JSONUtils.toJSON(queryParam));
        // 时间戳转换
        if(StringUtils.isNotBlank(queryParam.getStartTime())) {
            queryParam.setStartTime(DateUtil.getDateByTime(Long.valueOf(queryParam.getStartTime()), DateUtil.DATEFORMATDAY));
        }
        if(StringUtils.isNotBlank(queryParam.getEndTime())) {
            queryParam.setEndTime(DateUtil.getDateByTime(Long.valueOf(queryParam.getEndTime()), DateUtil.DATEFORMATDAY));
        }
        log.info("PayAmountReportApiImpl#pageByGroupParam start. param:{}", JSONUtils.toJSON(queryParam));
        PayAmountReportQueryParam param = new PayAmountReportQueryParam();
        param.setBusinessType(queryParam.getBusinessType());
        param.setBusinessOrderType(queryParam.getBusinessOrderType());
        param.setPayChannel(queryParam.getPayChannel());
        param.setStartTime(queryParam.getStartTime());
        param.setEndTime(queryParam.getEndTime());
        param.setLimit(queryParam.getPageSize());
        param.setOffset((queryParam.getPageNum()-1)*queryParam.getPageSize());

        List<PayAmountReport> payAmountReports = payAmountReportService.pageByGroupParam(param);

        long total = payAmountReportService.getTotal(param);

        List<PayAmountReportBO> data = new ArrayList<>();
        for (PayAmountReport payAmountReport : payAmountReports) {
            PayAmountReportBO payAmountReportBO = new PayAmountReportBO();
            BeanUtils.copyProperties(payAmountReport, payAmountReportBO);
            payAmountReportBO.setCountTime(queryParam.getStartTime()+" => "+queryParam.getEndTime());
            data.add(payAmountReportBO);
        }

        log.info("PayAmountReportApiImpl#pageByGroupParam end. resultData:{}", JSONUtils.toJSON(data));
        return ResultPageVo.createSuccess(queryParam.getPageNum(),queryParam.getPageSize(), total, data);
    }

}


